/*
 * Copyright 2008 [rapid-framework], Inc. All rights reserved.
 * Website: http://www.rapid-framework.org.cn
 */

define("pages/jls_ldsp/main", [
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        "../../plugins/artTemplate/artTemplate",
        "../../common/tools",
        "../../common/kss",
        "../../common/componts",
        "../../pages/jls_ldsp/form",
        "../../pages/jls_Jbxx/JbxxShowHide.html.js",
        "../../pages/jls_Ldsp/LdspShowHide.html.js",
    ],
    function (require) {
        var comonts = require("../../common/componts");
        var LdspForm = require("../../pages/jls_ldsp/form");
        var tmp = require("../../pages/jls_Jbxx/JbxxShowHide.html");
        var tmpLdsp = require("../../pages/jls_Ldsp/LdspShowHide.html");
        var kss=require("../../common/kss");
        var ryjbxxId="jbxxId";
        var ldspId = "ldspId";
        var selectid;
        var top = "30px";
        var ldsp = 'ldsp';
        var id;
        var jbxx = "jbxx";

        var initTable = function () {
            comonts.initTable(ldspId, {
                title: '领导审批',
                url: "/jls_ldsp/ldspList",
                toolbar: '#tool',
                firstLoad: true,
                pageSize: 20,
                columns: [
                    {
                        field: 'oid', title: '', formatter: function (value, rowData, rowIndex) {
                            return '<input type="radio" name="ldspIdRadio" id="ldspIdRadio"' + rowIndex + 'value="' + rowData.oid + '" />';
                        }
                    },
                    {
                        field: 'jsbh',
                        width: 150,
                        title: '监所编号',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.jsbh == null ? "" : row.jsbh) + '</span>'
                        }
                    },
                    {
                        field: 'rybh',
                        width: 150,
                        title: '人员编号',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.rybh == null ? "" : row.rybh) + '</span>'
                        }
                    },
                    {
                        field: 'sqr',
                        width: 150,
                        title: '申请人',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.sqr == null ? "" : row.sqr) + '</span>'
                        }
                    },
                    {
                        field: 'sqyy',
                        width: 150,
                        title: '申请原因',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.sqyy == null ? "" : row.sqyy) + '</span>'
                        }
                    },
                    {
                        field: 'sqsj',
                        width: 150,
                        title: '申请日期',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.sqsj == null ? "" : row.sqsj) + '</span>'
                        }
                    },
                    {
                        field: 'psbz',
                        width: 150,
                        title: '审批意见(批示结果)',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.psbz == null ? "" : row.psbz) + '</span>'
                        }
                    },
                    {
                        field: 'ldyj',
                        width: 150,
                        title: '领导批示意见',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.ldyj == null ? "" : row.ldyj) + '</span>'
                        }
                    },
                    {
                        field: 'blr',
                        width: 150,
                        title: '办理人',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.blr == null ? "" : row.blr) + '</span>'
                        }
                    },
                    {
                        field: 'state',
                        width: 150,
                        title: '启用状态',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.state == null ? "" : row.state) + '</span>'
                        }
                    },
                    {
                        field: 'createtime',
                        width: 150,
                        title: '创建时间',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.createtime == null ? "" : row.createtime) + '</span>'
                        }
                    },
                    {
                        field: 'updatetime',
                        width: 150,
                        title: '更新时间',
                        sortable: 'true',
                        formatter: function (value, row, index) {
                            return '<span title=' + value + '>' + (row.updatetime == null ? "" : row.updatetime) + '</span>'
                        }
                    },
                ],
                detailFormatter: function (index, rowData) {
                    return '<table style="height:40px;width:100%"><tr><td><strong></strong></td></tr></table>'
                },
                onClickRow: function (rowIndex, rowData) {
                    if (selectid != rowData.id) {
                        $("input[name='ldspIdRadio']")[rowIndex].checked = true;
                        $('#' + ldspId).datagrid("selectRow", rowIndex);
                        selectid = rowData.id;
                    } else if (selectid == rowData.id) {
                        $('#' + ldspId).datagrid("unselectRow", rowIndex);
                        $("input[name='ldspIdRadio']")[rowIndex].checked = false;
                        selectid = "";
                    }
                },
                //双击事件
                onDblClickRow: function (rowIndex, rowData) {
                    $("input[name='swdjRadio']")[rowIndex].checked = true;
                    $('#' + swdjId).datagrid("selectRow", rowIndex);
                    id = rowData.id;
                    var jbxx = $('#jbxxId').datagrid('getSelected');
                    swdjForm.show(jbxx, rowData, refreshTable);

                },
            });
            $('#ldspId').datagrid('resize', {
                height: ($(window).height() - 35)
            });
        }
        var findLdsp = function () {
           /* var val = $("#jqjsTree").tree('getSelected');
            var s_rybh = $("#s_rybh").textbox('getValue');
            var s_xm = $("#s_xm").textbox('getValue');*/
            //人员状态,单选框
            var opts = $('#' + ldspId).datagrid("options");
            opts.url = "/jls_jbxx/jbxxList";
            $('#' + ldspId).datagrid('clearSelections');
            $('#' + ldspId).datagrid('load', {
            });
        }

        var initJsTree = function () {
            kss.initJsTree("jqjsTree", "jbxxId", {});
        }

        var bindAction = function () {
            //查询
            $("#ldsp_search").on("click", function () {
                findLdsp();
            });
            //新增
            $("#ldsp_add").on("click", function () {
                var Ldsps = $("#" + ldspId).datagrid('getSelected');
                if (Ldsps) {
                    LdspForm.show(findLdsp, Ldsps);
                } else {
                    $.messager.alert("提示", "请选择要办理的人员!", "info");
                }
            });
            //修改
            $("#ldsp_update").on("click", function () {
                var Ldsp = $("#" + ldspId).datagrid('getSelected');
                if (Ldsp) {
                    LdspForm.show(findLdsp, Ldsp);
                } else {
                    $.messager.alert("提示", "请选择要修改的记录!", "info");
                }
            });
            //删除
            $("#ldsp_remove").on("click", function () {
                //把你选中的 数据查询出来。
                var selectRows = $('#ldspId').datagrid("getSelections");
                if (selectRows == null || selectRows == undefined) {

                    return $.messager.alert("提示消息", "请选中要删的数据!");
                } else {
                    $.messager.confirm("操作提示", "您确定要删除该条记录吗？", function (data) {

                        if (data) {
                            submitLoad();
                            $.ajax({
                                type: "POST",  //提交方式
                                url: "/jls_ldsp/ldspDelete",//路径
                                data: {id: selectRows[0].id},//数据，这里使用的是Json格式进行传输
                                success: function (result) {
                                    if (result.success) {
                                        $.messager.alert("确定", "删除成功", "info");
                                        findLdsp();
                                    } else {
                                        removeLoad();
                                        $.messager.alert("确定", "删除失败", "info");
                                    }
                                },
                                complete: function () {
                                    removeLoad();
                                },
                            });
                        } else {
                            return false;
                        }

                    });

                }
            });
            comonts.ShowHide(ldsp, tmp);
        }
        var initView = function () {
            initTable();
            bindAction();
            initJsTree();
        }
        initView();

    });